package com.weaver.seconddev.custom.qc1853114.util;

import com.sansec.devicev4.sfot.Util;
import com.sansec.devicev4.sfot.sm4.SM4;
import com.sansec.devicev4.sfot.sm4.SM4_Context;
import com.weaver.verupgrade.file.AESCoder;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Slf4j
public class SM4Utils {

    private boolean hexString = false;
    private static String ENC_MODE = "AES";
    public static final String ENC_AES = "AES";
    public static final String ENC_SM4 = "SM4";

    public String createSecretKey() {
        return UUID.randomUUID().toString().substring(0, 16);
    }

    public String encrypt(String var1, String var2) {
        return this.encrypt((String)var1, var2, (String)null);
    }

    public String encrypt(String var1, String var2, String var3) {
        try {
            if ("SM4".equalsIgnoreCase(var3) || ENC_MODE.equalsIgnoreCase("SM4") && !"AES".equalsIgnoreCase(var3)) {
                SM4_Context var4 = new SM4_Context();
                var4.isPadding = true;
                var4.mode = 1;
                byte[] var5;
                if (this.hexString) {
                    var5 = Util.hexStringToBytes(var2);
                } else {
                    var5 = var2.getBytes();
                }

                SM4 var6 = new SM4();
                var6.sm4_setkey_enc(var4, var5);
                byte[] var7 = var6.sm4_crypt_ecb(var4, var1.getBytes("UTF-8"));
                String var8 = (new BASE64Encoder()).encode(var7);
                if (var8 != null && var8.trim().length() > 0) {
                    Pattern var9 = Pattern.compile("\\s*|\t|\r|\n");
                    Matcher var10 = var9.matcher(var8);
                    var8 = var10.replaceAll("");
                }

                return var8;
            } else {
                return AESCoder.encrypt(var1, var2);
            }
        } catch (Exception var11) {
            log.error("Exception"+var11);
            var11.printStackTrace();
            return null;
        }
    }


    public String decrypt(String var1, String var2) {
        return this.decrypt(var1, var2, (String)null);
    }

    public String decrypt(String var1, String var2, String var3) {
        try {
            if (!"SM4".equalsIgnoreCase(var3) && (!ENC_MODE.equalsIgnoreCase("SM4") || "AES".equalsIgnoreCase(var3))) {
                return decrypt1(var1, var2);
            } else {
                SM4_Context var4 = new SM4_Context();
                var4.isPadding = true;
                var4.mode = 0;
                byte[] var5;
                if (this.hexString) {
                    var5 = Util.hexStringToBytes(var2);
                } else {
                    var5 = var2.getBytes();
                }

                SM4 var6 = new SM4();
                var6.sm4_setkey_dec(var4, var5);
                byte[] var7 = var6.sm4_crypt_ecb(var4, (new BASE64Decoder()).decodeBuffer(var1));
                return new String(var7, "UTF-8");
            }
        } catch (Exception var8) {
            var8.printStackTrace();
            return null;
        }
    }

    public static String decrypt1(String var0, String var1) throws Exception {
        try {
            if (var1 == null) {
                return null;
            } else {
                byte[] var2 = initSecretKey(var1);
                SecretKeySpec var3 = new SecretKeySpec(var2, "AES");
                Cipher var4 = Cipher.getInstance("AES");
                var4.init(2, var3);
                byte[] var5 = hex2byte(var0);

                try {
                    byte[] var6 = var4.doFinal(var5);
                    String var7 = new String(var6);
                    return var7;
                } catch (Exception var8) {
                    var8.printStackTrace();
                    return null;
                }
            }
        } catch (Exception var9) {
            var9.printStackTrace();
            return null;
        }
    }

    public static byte[] initSecretKey(String var0) {
        KeyGenerator var1 = null;

        try {
            var1 = KeyGenerator.getInstance("AES");
            SecureRandom var2 = SecureRandom.getInstance("SHA1PRNG");
            var2.setSeed(var0.getBytes());
            var1.init(128, var2);
        } catch (NoSuchAlgorithmException var3) {
            var3.printStackTrace();
            return new byte[0];
        }

        SecretKey var4 = var1.generateKey();
        return var4.getEncoded();
    }

    public static byte[] hex2byte(String var0) {
        if (var0 == null) {
            return null;
        } else {
            int var1 = var0.length();
            if (var1 % 2 == 1) {
                return null;
            } else {
                byte[] var2 = new byte[var1 / 2];

                for(int var3 = 0; var3 != var1 / 2; ++var3) {
                    var2[var3] = (byte)Integer.parseInt(var0.substring(var3 * 2, var3 * 2 + 2), 16);
                }

                return var2;
            }
        }
    }

}
